/**
 * @author Matthew.He
 */
package com.iot.ic.modules.dao.impl;

import com.iot.ic.common.dao.BaseDaoImpl;
import com.iot.ic.modules.dao.BankDao;
import com.iot.ic.modules.model.UserBank;
import org.springframework.stereotype.Service;

import javax.persistence.EntityManager;
import javax.persistence.Query;
import java.util.List;

/**
 *
 */
@Service("bankDao")
public class BankDaoImpl extends BaseDaoImpl implements BankDao {

    @Override
    public UserBank findBankByNo(String bankNo) {
        EntityManager em = super.getEntiyManager();
        Query query = em.createNativeQuery(" select t.*  from t_user_bank t where t.bank_card_no=?1 ", UserBank.class);
        query.setParameter(1, bankNo);
        List list = query.getResultList();
        Object outO;
        if (list == null || list.size() == 0) {
            outO = null;
        } else {
            outO = list.get(0);
        }
        em.close();
        return (UserBank) outO;
    }

    @Override
    public List<UserBank> findBank(Integer id, Integer userId, String bankNo) throws Exception {
        StringBuffer sql = new StringBuffer();
        sql.append(" select t.*  from t_user_bank t where 1=1 ");
        if (id != null) {
            sql.append(" and id =").append(id);
        }
        if (userId != null) {
            sql.append(" and user_Id =").append(userId);
        }
        if (bankNo != null) {
            sql.append(" and bank_card_no = '").append(bankNo+"'");
        }
        EntityManager em = super.getEntiyManager();
        Query query = em.createNativeQuery(sql.toString(), UserBank.class);
        List list = query.getResultList();
        em.close();
        return list;
    }
}
